import { addAssets } from '@/services/ant-design-pro/api';
import { PlusOutlined } from '@ant-design/icons';
import { ActionType, ModalForm, ProFormText, ProFormSelect,ProFormDateTimePicker,ProFormDigit } from '@ant-design/pro-components';
import { FormattedMessage, useIntl, useRequest } from '@umijs/max';
import { Button, message } from 'antd';
import { FC } from 'react';
import { fetchassetsvaluelable } from '../../../../utility/fetchcategoryforselect';
interface CreateFormProps {
  reload?: ActionType['reload'];
}

const CreateForm: FC<CreateFormProps> = (props) => {
  const { reload } = props;

  const [messageApi, contextHolder] = message.useMessage();
  /**
   * @en-US International configuration
   * @zh-CN 国际化配置
   * */
  const intl = useIntl();

  const { run, loading } = useRequest(addAssets, {
    manual: true,
    onSuccess: () => {
      messageApi.success('物资添加成功');
      reload?.();
    },
    onError: () => {
      messageApi.error('物资添加失败!');
    },
  });

  return (
    <>
      {contextHolder}
      <ModalForm
        title={intl.formatMessage({
          id: 'pages.assets.addForm.title',
          defaultMessage: '添加资产信息',
        })}
        //这里就是新建的按钮
        trigger={
          <Button type="primary" icon={<PlusOutlined />}>
            <FormattedMessage id="pages.new" defaultMessage="新建" />
          </Button>
        }
        width="400px"
        modalProps={{ okButtonProps: { loading } }}
        onFinish={async (value) => {
          const formattedDate = new Date(value.time);
          value.time = formattedDate;
          await run(value as API.AssetsListItem );
          return true;
        }}
      >
        <ProFormSelect
            name="assetsclass"
            width="md"
            label={intl.formatMessage({
              id: 'pages.assets.Form.assetscalss',
              defaultMessage: '资产类别',
            })}
            request={async () => {
              // 可以根据需要的逻辑来选择 categoryClass  
              return fetchassetsvaluelable(1);
            }}
            // valueEnum={{
            //   1: '设备',
            //   2: '传感器',
            //   3: '工装',
            //   4: '线缆',
            // }}
          />
          <ProFormSelect
            name="assetsname"
            width="md"
            label={intl.formatMessage({
              id: 'pages.assets.Form.assetsname',
              defaultMessage: '资产名称',
            })}
            request={async () => {
              // 可以根据需要的逻辑来选择 categoryClass  
              return fetchassetsvaluelable(7);
            }}
            // valueEnum={{
            //   1: 'EDAQ',
            //   2: 'EDAQLite',
            //   3: 'WFT',
            //   4: 'GPS',
            // }}
          />
          <ProFormText
            name="sn"
            label={intl.formatMessage({
              id: 'pages.assets.Form.sn',
              defaultMessage: '规则名称',
            })}
            width="md"
            rules={[
              {
                required: true,
                message: (
                  <FormattedMessage
                    id="pages.assets.Form.ruleName.sn"
                    defaultMessage="请输入序列号！"
                  />
                ),
              },
            ]}
          />
          <ProFormText
            name="assetsnumber"
            label={intl.formatMessage({
              id: 'pages.assets.Form.assetsnumber',
              defaultMessage: '规则名称',
            })}
            width="md"
            rules={[
              {
                required: true,
                message: (
                  <FormattedMessage
                    id="pages.assets.Form.ruleName.assetsnumber"
                    defaultMessage="请输入资产编号！"
                  />
                ),
              },
            ]}
          />
          <ProFormText
            name="type"
            label={intl.formatMessage({
              id: 'pages.assets.Form.type',
              defaultMessage: '规则名称',
            })}
            width="md"
            rules={[
              {
                required: true,
                message: (
                  <FormattedMessage
                    id="pages.assets.Form.ruleName.type"
                    defaultMessage="请输入型号！"
                  />
                ),
              },
            ]}
          />

          <ProFormDateTimePicker
            name="time"
            width="md"
            label={intl.formatMessage({
              id: 'pages.assets.Form.time',
              defaultMessage: '开始时间',
            })}
            rules={[
              {
                required: true,
                message: (
                  <FormattedMessage
                    id="pages.assets.Form.ruleName.time"
                    defaultMessage="请输入入库时间！"
                  />
                ),
              },
            ]}
          />
          <ProFormDigit
            name="calinterval"
            label={intl.formatMessage({
              id: 'pages.assets.Form.calinterval',
              defaultMessage: '标定间隔',
            })}
            width="md"
            rules={[
              {
                required: true,
                message: (
                  <FormattedMessage
                    id="pages.assets.Form.ruleName.calinterval"
                    defaultMessage="请输入标定间隔！"
                  />
                ),
              },
            ]}
          />
           <ProFormDigit
            name="count"
            label={intl.formatMessage({
              id: 'pages.assets.Form.count',
              defaultMessage: '数量',
            })}
            width="md"
            rules={[
              {
                required: true,
                message: (
                  <FormattedMessage
                    id="pages.assets.Form.ruleName.count"
                    defaultMessage="请输入数量！"
                  />
                ),
              },
              //确保输入为整数
              {  
                validator: (_, value) => {  
                  if (value !== undefined && !Number.isInteger(value)) {  
                    return Promise.reject(  
                      <FormattedMessage  
                        id="pages.assets.Form.ruleName.countInteger"  
                        defaultMessage="请输入整数！"  
                      />  
                    );  
                  }  
                  return Promise.resolve();  
                },  
              },  
            ]}
           
          />
      </ModalForm>
    </>
  );
};

export default CreateForm;
